﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Irandeb4.Models;

namespace Irandeb4.Controllers
{
    [Authorize(Roles = "Administrator , Employee")]
    public class ProductManagerController : Controller
    {
        private IrandebEntities db = new IrandebEntities();

        //
        // GET: /ProductManager/

        //
        // GET: /ProductManager/Details/5

        public ViewResult Details(int id)
        {
            Product product = db.Products.Find(id);
            return View(product);
        }

        //
        // GET: /ProductManager/Create

        public ActionResult Create()
        {
            IEnumerable<Company> suppliers = from c in db.companies
                            where c.Coustomer == false
                            select c;
            ViewBag.Suppliers = new SelectList(suppliers,"CompanyId","Name");
            return View();
        } 

        //
        // POST: /ProductManager/Create

        [HttpPost]
        public ActionResult Create(Product product)
        {            
            if (ModelState.IsValid)
            {                
                db.Products.Add(product);
                db.SaveChanges();
                return RedirectToAction("Search");  
            }
            IEnumerable<Company> suppliers = from c in db.companies
                                             where c.Coustomer == false
                                             select c;
            ViewBag.Suppliers = new SelectList(suppliers);
            return View(product);
        } 
        
        //
        // GET: /ProductManager/Edit/5
 
        public ActionResult Edit(int id)
        {
            Product product = db.Products.Find(id);
            return View(product);
        }

        //
        // POST: /ProductManager/Edit/5

        [HttpPost]
        public ActionResult Edit(Product product)
        {
            if (ModelState.IsValid)
            {                
                Product productToSave = db.Products.Find(product.ProductId);
                productToSave.Name = product.Name;
                productToSave.Type = product.Type;
                productToSave.Description = product.Description;
                
                db.SaveChanges();
                return RedirectToAction("Search");
            }
            return View(product);
        }

        //
        // GET: /ProductManager/Delete/5
 
        public ActionResult Delete(int id)
        {
            Product product = db.Products.Find(id);
            return View(product);
        }

        //
        // POST: /ProductManager/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Product product = db.Products.Find(id);
            db.Products.Remove(product);
            db.SaveChanges();
            return RedirectToAction("Search");
        }

        public ActionResult Search()
        {
            var products = db.Products;
            return View(products.ToList());
        }

        public ActionResult SeeSuppliers(int id)
        {
            var supplies = db.Supplies.Find(id);
            return View(supplies.suppliers.ToList());
        }
                
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
